{% extends 'adminuser/base_admin.html' %}

{% block admin_title %}管理日志{% endblock %}

{% block admin_content %}
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-header bg-light d-flex justify-content-between align-items-center">
                <h5 class="mb-0">管理日志</h5>
                <div class="input-group" style="width: 300px;">
                    <input type="text" id="log-search" class="form-control" placeholder="搜索日志...">
                    <button class="btn btn-outline-secondary" type="button" id="search-btn">
                        <i class="bi bi-search"></i>
                    </button>
                </div>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <form method="get" class="row g-3">
                        <div class="col-md-3">
                            <select name="action_type" class="form-select">
                                <option value="">所有操作类型</option>
                                <option value="LOGIN" {% if request.GET.action_type == 'LOGIN' %}selected{% endif %}>登录</option>
                                <option value="LOGOUT" {% if request.GET.action_type == 'LOGOUT' %}selected{% endif %}>登出</option>
                                <option value="CREATE" {% if request.GET.action_type == 'CREATE' %}selected{% endif %}>创建</option>
                                <option value="UPDATE" {% if request.GET.action_type == 'UPDATE' %}selected{% endif %}>更新</option>
                                <option value="DELETE" {% if request.GET.action_type == 'DELETE' %}selected{% endif %}>删除</option>
                                <option value="SETTINGS" {% if request.GET.action_type == 'SETTINGS' %}selected{% endif %}>设置</option>
                                <option value="OTHER" {% if request.GET.action_type == 'OTHER' %}selected{% endif %}>其他</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <input type="date" name="date_from" class="form-control" 
                                   value="{{ request.GET.date_from }}" placeholder="开始日期">
                        </div>
                        <div class="col-md-3">
                            <input type="date" name="date_to" class="form-control" 
                                   value="{{ request.GET.date_to }}" placeholder="结束日期">
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="bi bi-filter me-1"></i> 筛选
                            </button>
                        </div>
                    </form>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>管理员</th>
                                <th>操作类型</th>
                                <th>详情</th>
                                <th>IP地址</th>
                                <th>时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in logs %}
                            <tr>
                                <td>{{ log.id }}</td>
                                <td>{{ log.user.username }}</td>
                                <td>
                                    {% if log.action == 'LOGIN' %}
                                    <span class="badge bg-primary">登录</span>
                                    {% elif log.action == 'LOGOUT' %}
                                    <span class="badge bg-secondary">登出</span>
                                    {% elif log.action == 'CREATE' %}
                                    <span class="badge bg-success">创建</span>
                                    {% elif log.action == 'UPDATE' %}
                                    <span class="badge bg-info">更新</span>
                                    {% elif log.action == 'DELETE' %}
                                    <span class="badge bg-danger">删除</span>
                                    {% elif log.action == 'SETTINGS' %}
                                    <span class="badge bg-warning">设置</span>
                                    {% else %}
                                    <span class="badge bg-light text-dark">其他</span>
                                    {% endif %}
                                </td>
                                <td>{{ log.details }}</td>
                                <td>{{ log.ip_address }}</td>
                                <td>{{ log.created_at|date:"Y-m-d H:i:s" }}</td>
                                <td>
                                    <button type="button" class="btn btn-sm btn-outline-primary view-details" 
                                            data-id="{{ log.id }}" data-bs-toggle="modal" data-bs-target="#logModal">
                                        <i class="bi bi-info-circle"></i>
                                    </button>
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="7" class="text-center py-4">
                                    <div class="text-muted">
                                        <i class="bi bi-exclamation-circle me-2"></i>没有找到日志记录
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                {% if logs.has_other_pages %}
                <nav aria-label="Page navigation" class="mt-4">
                    <ul class="pagination justify-content-center">
                        {% if logs.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% if request.GET.action_type %}&action_type={{ request.GET.action_type }}{% endif %}{% if request.GET.date_from %}&date_from={{ request.GET.date_from }}{% endif %}{% if request.GET.date_to %}&date_to={{ request.GET.date_to }}{% endif %}" aria-label="First">
                                <span aria-hidden="true">&laquo;&laquo;</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ logs.previous_page_number }}{% if request.GET.action_type %}&action_type={{ request.GET.action_type }}{% endif %}{% if request.GET.date_from %}&date_from={{ request.GET.date_from }}{% endif %}{% if request.GET.date_to %}&date_to={{ request.GET.date_to }}{% endif %}" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        {% else %}
                        <li class="page-item disabled">
                            <a class="page-link" href="#" aria-label="First">
                                <span aria-hidden="true">&laquo;&laquo;</span>
                            </a>
                        </li>
                        <li class="page-item disabled">
                            <a class="page-link" href="#" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        {% endif %}
                        
                        {% for i in logs.paginator.page_range %}
                            {% if logs.number == i %}
                            <li class="page-item active"><a class="page-link" href="#">{{ i }}</a></li>
                            {% elif i > logs.number|add:'-3' and i < logs.number|add:'3' %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ i }}{% if request.GET.action_type %}&action_type={{ request.GET.action_type }}{% endif %}{% if request.GET.date_from %}&date_from={{ request.GET.date_from }}{% endif %}{% if request.GET.date_to %}&date_to={{ request.GET.date_to }}{% endif %}">{{ i }}</a>
                            </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if logs.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ logs.next_page_number }}{% if request.GET.action_type %}&action_type={{ request.GET.action_type }}{% endif %}{% if request.GET.date_from %}&date_from={{ request.GET.date_from }}{% endif %}{% if request.GET.date_to %}&date_to={{ request.GET.date_to }}{% endif %}" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ logs.paginator.num_pages }}{% if request.GET.action_type %}&action_type={{ request.GET.action_type }}{% endif %}{% if request.GET.date_from %}&date_from={{ request.GET.date_from }}{% endif %}{% if request.GET.date_to %}&date_to={{ request.GET.date_to }}{% endif %}" aria-label="Last">
                                <span aria-hidden="true">&raquo;&raquo;</span>
                            </a>
                        </li>
                        {% else %}
                        <li class="page-item disabled">
                            <a class="page-link" href="#" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                        <li class="page-item disabled">
                            <a class="page-link" href="#" aria-label="Last">
                                <span aria-hidden="true">&raquo;&raquo;</span>
                            </a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 日志详情模态框 -->
<div class="modal fade" id="logModal" tabindex="-1" aria-labelledby="logModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="logModalLabel">日志详情</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3 row">
                    <label class="col-sm-3 col-form-label fw-bold">ID:</label>
                    <div class="col-sm-9">
                        <p class="form-control-plaintext" id="log-id"></p>
                    </div>
                </div>
                <div class="mb-3 row">
                    <label class="col-sm-3 col-form-label fw-bold">管理员:</label>
                    <div class="col-sm-9">
                        <p class="form-control-plaintext" id="log-user"></p>
                    </div>
                </div>
                <div class="mb-3 row">
                    <label class="col-sm-3 col-form-label fw-bold">操作类型:</label>
                    <div class="col-sm-9">
                        <p class="form-control-plaintext" id="log-action"></p>
                    </div>
                </div>
                <div class="mb-3 row">
                    <label class="col-sm-3 col-form-label fw-bold">IP地址:</label>
                    <div class="col-sm-9">
                        <p class="form-control-plaintext" id="log-ip"></p>
                    </div>
                </div>
                <div class="mb-3 row">
                    <label class="col-sm-3 col-form-label fw-bold">时间:</label>
                    <div class="col-sm-9">
                        <p class="form-control-plaintext" id="log-time"></p>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label fw-bold">详细信息:</label>
                    <div class="card">
                        <div class="card-body bg-light">
                            <pre id="log-details" class="mb-0" style="white-space: pre-wrap;"></pre>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 日志详情模态框
    const viewDetailsButtons = document.querySelectorAll('.view-details');
    viewDetailsButtons.forEach(button => {
        button.addEventListener('click', function() {
            const logId = this.getAttribute('data-id');
            // 这里应该使用AJAX获取日志详情
            // 演示用：直接从页面获取数据
            const row = this.closest('tr');
            const cells = row.querySelectorAll('td');
            
            document.getElementById('log-id').textContent = cells[0].textContent;
            document.getElementById('log-user').textContent = cells[1].textContent;
            document.getElementById('log-action').textContent = cells[2].textContent.trim();
            document.getElementById('log-details').textContent = cells[3].textContent;
            document.getElementById('log-ip').textContent = cells[4].textContent;
            document.getElementById('log-time').textContent = cells[5].textContent;
        });
    });
    
    // 日志搜索
    const searchBtn = document.getElementById('search-btn');
    const searchInput = document.getElementById('log-search');
    
    searchBtn.addEventListener('click', function() {
        const searchText = searchInput.value.toLowerCase();
        const tableRows = document.querySelectorAll('tbody tr');
        
        tableRows.forEach(row => {
            const text = row.textContent.toLowerCase();
            if (text.includes(searchText)) {
                row.style.display = '';
            } else {
                row.style.display = 'none';
            }
        });
    });
    
    // 按回车键搜索
    searchInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            searchBtn.click();
        }
    });
});
</script>
{% endblock %} 